package com.hbnu.collection;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author kittychen
 * @since 2025-10-14 14:39
 */
public class HashSetDemo {
    public static void main(String[] args) {
        Set<String> hashSet = new HashSet<>();

        hashSet.add("element8");
        hashSet.add("element2");
        hashSet.add("element4");
        hashSet.add("element1");
        hashSet.add("element6");
        hashSet.add("element3");
        hashSet.add("element8");
        System.out.println(hashSet);

        System.out.println(hashSet.size());
        System.out.println(hashSet.isEmpty());
        System.out.println(hashSet.contains("element8"));
        System.out.println(hashSet.remove("element8"));

        // 增强for循环遍历set集合
        for (String s : hashSet) {
            System.out.println(s);
        }

        // 迭代器遍历set集合
        Iterator<String> iterator = hashSet.iterator();  // 获取集合迭代器
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
